/**
 * 
 */
package teehee.UI;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import teehee.Action.Action;
import teehee.DB.DB;
import teehee.Form.WodPageForm;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Main UI Driver for the WOD Page.
 * 
 * @author Alec
 */

public class WodPage extends Activity
{
	/**
	 * Field representing the Submit button at the bottom of the page
	 */
	protected Button submit;
	
	/**
	 * The EditText Object representing the <ins>Exercise</ins> text field
	 */
	
	private EditText exerciseBox;
	
	/**
	 * The EditText Object representing the <ins>Reps</ins> text field
	 */
	private EditText repsBox;
	
	/**
	 * The EditText Object representing the <ins>Time</ins> text field
	 */
	private EditText timeBox;
	
	/**
	 * The EditText Object representing the <ins>weights</ins> text field
	 */
	private EditText weightsBox;
	
	/**
	 * The EditText Object representing the <ins>comments</ins> text field
	 */
	private EditText commentsBox;

	/**
	 * The EditText Object representing the <ins>comments</ins> text field
	 */
	protected TextView exerciseDescBox;
	
	
	
	/**
	 * The String representing text in our exercise field.
	 */
	protected static String exercise;
	
	/**
	 * The String representing text in our exercise description text view (e.g. JLabel).
	 */
	protected static String exerciseDesc = "asdfasdf";
	
	/**
	 * The String representing text in our reps field.
	 */
	protected static String reps;
	
	/**
	 * The String representing text in our time field.
	 */
	protected static String time;
	
	/**
	 * The String representing text in our weights field.
	 */
	protected static String weights;
	
	/**
	 * The String representing text in our comments field.
	 */
	protected static String comments;

	public static DB entry;
	public static boolean fromWodPage;
	
	/**
	 * Called when this UI interface is instantiated.  At the moment, this method assigns buttons and fields
	 * with layout values found in R.java.
	 * 
	 * @param savedInstanceState
	 * 
	 */

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.wod_page); //setting the layout
        
        exerciseBox = (EditText)findViewById(R.id.editText1);
        repsBox = (EditText)findViewById(R.id.editText2);
        timeBox = (EditText)findViewById(R.id.editText3);
        weightsBox = (EditText)findViewById(R.id.editText4);
        commentsBox = (EditText)findViewById(R.id.editText5);
        exerciseDescBox = (TextView)findViewById(R.id.exDesc);
        
        submit = (Button) findViewById(R.id.wodsubmitbutton);
        submit.setOnClickListener( new ButtonClickHandler() );
      
        if(ExerciseHistory.fromHistory)
        {
        	exerciseBox.setText(Action.convertBack(ExerciseHistory.input.getName()));
        	exerciseDescBox.setVisibility(TextView.VISIBLE);
        	//ask connie to provide a getDesc method for you
        	//She should be easily able to do that.
        	
        	exerciseDescBox.setText(ExerciseHistory.descpt);
        	ExerciseHistory.fromHistory = false;
        }
    }
        
	/**
	 * Sets the fields of the WodPage UI.  Bridge method for ExerciseHistory.java.
	 * 
	 * @param ex The name of the exercise that the exercise field shall be set to.
	 * @param desc The description for the exercise field we just set.
	 * @param rs The value to be set to the reps field.
	 * @param tm The value to be set to the time field.
	 * @param w The value to be set to the weights field.
	 * @param cmts The value to be set to the comments field.
	 * 
	 * @see teehee.UI.ExerciseHistory
	 * 
	 */
        public static void setFields(String ex, String desc, String rs, String tm, String w, String cmts)
        {
        	exercise = ex;
        	exerciseDesc = desc;
        	reps = rs;
        	time = tm;
        	weights = w;
        	comments = cmts;	
        }

        
	/**
	 * Subclass that manages user-clicks of the Submit button.  Will Toast user if something is wrong, or submit entered
	 * values to database if everything passes validation.
	 * 
	 * @author Alec
	 * 
	 */
    
    	protected class ButtonClickHandler implements View.OnClickListener 
    	{
    		boolean contflag = true;
    		
    		/**
    		 * The method that is executed when the user clicks the Submit button. Does error checking and validation,
    		 * as well as submitting to the database.
    		 * 
    		 * @param view
    		 */
    		
    		public void onClick( View view ) 
    		{
    			
    			WodPage.setFields(exerciseBox.getText().toString(), exerciseDesc, repsBox.getText().toString(), timeBox.getText().toString(), weightsBox.getText().toString(), commentsBox.getText().toString());
    			
    		/*
    			String name = Action.convertToDbFormat(.getText().toString());
    			String r = Action.convertToDbFormat(reps.getText().toString());
    			String t = Action.convertToDbFormat(time.getText().toString());
    			String w = Action.convertToDbFormat(weights.getText().toString());
    			String com = Action.convertToDbFormat(comments.getText().toString());
    		*/
    			entry = WodPageForm.processInput(exercise);
    			
    			if(entry == null)
    				toaster("WOD was not found in database. Please add it first before using it.");
    			else if(!WodPageForm.validateRepsField(reps))
    				toaster("Reps must consist of a blank field, or numbers in the format 'dd', 'dd-dd', 'dd-dd-dd', etc.");
    			else if(!WodPageForm.validateTime(time))
    				toaster("Time must be in format ':ss', 'mm:ss', 'hh:mm:ss'.  No fields may exceed 60.");
    			else if(!WodPageForm.validateWeight(weights))
    				toaster("Weight field must consist of numerical digits only. No decimals allowed.");
    			else if(!WodPageForm.validateGeneralField(comments))
    				toaster("Comments Field must consist of characters between a-z, 0-9, and spaces.");
    			else
    		{
     			Calendar c = Calendar.getInstance();
    			SimpleDateFormat f2 = new SimpleDateFormat("MMddyyyy");
    			String date = f2.format(c.getTime());
    			WodPageForm.createEntry(getApplicationContext(), date, reps, time, weights,comments,entry);
    			fromWodPage = true;
    			Intent exHist = new Intent(WodPage.this, ExerciseHistory.class);
  		        startActivity(exHist);
    		}
    	}
    		
    		/**
    		 * Generic method to toast the user with a specific error message when something goes wrong.
    		 * 
    		 * @param errormessage The error message to display.
    		 */
    		
    		protected void toaster(String errormessage)
    		{
    			contflag = false; 
				Context context = getApplicationContext();
				Toast toast = Toast.makeText(context, errormessage, Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.TOP|Gravity.LEFT, 25, 300);
				toast.show();
				
    		}
    		
    	}
    	
    	@Override 
	    public boolean onKeyDown(int keyCode, KeyEvent event) 
	    {
	        if ((keyCode == KeyEvent.KEYCODE_MENU))
	        { 
	        	//setContentView(R.layout.main);
	        	Intent homep = new Intent(WodPage.this, MainPage.class);
	    		startActivity(homep);
	            return true;
	        }
	        
	        if ((keyCode == KeyEvent.KEYCODE_SEARCH))
	        { 
	        	//setContentView(R.layout.main);
	        	Intent homep = new Intent(WodPage.this, SearchPage.class);
	    		startActivity(homep);
	            return true;
	        }
	        
	        return super.onKeyDown(keyCode, event);    
	    } 
    }
        

